<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$bullionParty = array();
$partysFine   = 0;
$partysAmount = 0;
$fineTotal    = 0;
$amountTotal  = 0;

$selectBullinoParty = "SELECT partyId, bullionPartyName 
                         FROM bullionparty";
$selectBullinoPartyRes = mysql_query($selectBullinoParty);
$a = 0;
while($row = mysql_fetch_assoc($selectBullinoPartyRes))
{
  $bullionParty[$a]['id']     = $row['partyId'];
  $bullionParty[$a]['name']   = $row['bullionPartyName'];
  $bullionParty[$a]['kg']          = 0;
  $bullionParty[$a]['cramount']    = 0;
  $bullionParty[$a]['crfine']      = 0;
  $bullionParty[$a]['dramount']    = 0;
  $bullionParty[$a]['partyFine']   = 0;
  $bullionParty[$a]['partyAmount'] = 0;

  
	$bullionDetail = "SELECT bulliondetail.kg, bulliondetail.cramount, bulliondetail.crfine, bulliondetail.dramount
	                        FROM bullionmaster
	                        JOIN bulliondetail ON bulliondetail.bullionmasterid = bullionmaster.bullionmasterid
	                       WHERE bullionmaster.partyId = ".$row['partyId']."
	                       ORDER BY bullionmaster.bulliondate, bulliondetail.bulliondetailid";
	$bullionDetailRes = mysql_query($bullionDetail);
	while($bullionRow = mysql_fetch_assoc($bullionDetailRes))
	{
			$bullionParty[$a]['kg']          += $bullionRow['kg'];
			$bullionParty[$a]['cramount']    += $bullionRow['cramount'];
			$bullionParty[$a]['crfine']      += $bullionRow['crfine'];
			$bullionParty[$a]['dramount']    += $bullionRow['dramount'];
	    $bullionParty[$a]['partyFine']   += $bullionRow['kg'] - $bullionRow['crfine'];
	    $bullionParty[$a]['partyAmount'] += $bullionRow['dramount'] - $bullionRow['cramount'];
	}
	
	$fineTotal   += $bullionParty[$a]['partyFine'];
	$amountTotal += $bullionParty[$a]['partyAmount'];
	$a++;
}

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_total",$l_total);

$smarty->assign("bullionParty",$bullionParty);
$smarty->assign("partysFine",$partysFine);
$smarty->assign("partysAmount",$partysAmount);
$smarty->assign("fineTotal",$fineTotal);
$smarty->assign("amountTotal",$amountTotal);
$smarty->display("bullionSummary.tpl");
?>